<?php
/**
 * Created by PhpStorm.
 * User: Inhere
 * Date: 2015/1/29
 * Time: 22:37
 * Use : 语言服务
 * File: Language.php
 */

namespace ulue\base\language;

use Ioc;
use Ulue;
use ulue\libs\helpers\PhpHelper;
use ulue\libs\datastorage\AbstractDatastorage;

class Language extends AbstractDatastorage
{
    /**
     * @override \ulue\libs\datastorage\traits\TraitDataOperator::checkPath
     * @var boolean
     */
    protected $checkPath = false;

    /**
     * 默认语言
     * @var string
     */
    private $defaultLang      = 'en';

    /**
     * 使用的语言
     * @var string
     */
    protected $lang;

    /**
     * 语言包文件名
     * @var string
     */
    protected $filename = 'lang.php';

    /**
     * 系统语言包路径
     * @var string
     */
    protected $sysLangDir = '@system/i18n';

    /**
     * 语言包路径
     * @var string
     */
    protected $langDir = '@comm';

    /**
     * 实际上载入的语言包
     * @var string
     */
    protected $langFile;

    /**
     *
     * @param array $options [description]
     *  [
     *     'lang'     => $this->defaultLang,
     *     'langDir'  => '@comm',
     *     'filename' => '...',
     * ]
     */
    public function __construct(array $options)
    {
        $allows =  ['lang', 'langDir', 'filename', ];

        foreach ($options as $key => $value) {
            if ((string)$value && in_array($key, $allows) ) {
                $this->$key = $value;
            }
        }

        $this->loadPackage($this->lang);
    }

    public function set($key, $value, $createNode=true)
    {
        return parent::set($key, $value, true);
    }

    /**
     *
     * @param string $lang 语言 不要带后缀名 --> 语言文件夹 zh_cn/
     * @param string $format 文件格式  zh_cn/lang.php
     * @return $this|bool
     */
    public function loadPackage($lang)
    {
        if (!$lang || !is_string($lang)) {
            return false;
        }

        // 默认语言不用加载
        if ($lang == $this->defaultLang) {
            return true;
        }

        $loadFile = false;

        // 系统语言包路径
        $defaultLangFile  = \Ulue::resolveAlias($this->sysLangDir) . "/$lang/lang.php";

        // 先检查是否有同名系统包，先加载 默认数据，以补充包数据
        // 自定义包 则在系统包的基础上追加数据
        if ( file_exists($defaultLangFile) ) {
            $loadFile = true;
            $this->langFile = $defaultLangFile;

            $this->loadFile($defaultLangFile, 'php');
        }

        // 自定义语言包路径
        $langFile = Ulue::resolveAlias($this->langDir) . "/langs/$lang/" . $this->filename;

        // 加载自定义包
        if ( file_exists($langFile) ) {
            $loadFile = true;
            $this->langFile = $langFile;

            $this->loadFile($langFile, 'php');
        }

        if (!$loadFile) {
            throw new \DInvalidArgumentException("You set the language of the [{$lang}] corresponding language  pack file  don't exist!");
        }
    }

    public function tl( $string , array $repeat = [])
    {
        return $this->translation($string , $repeat);
    }

    public function translation( $string , array $repeat = [])
    {
        if ( $translation = $this->get(trim($string)) ) {
            $string = $translation;
        }

        $return = $repeat ? strtr($string, $repeat) : $string;

        if (PhpHelper::isCli()) {
            $return = str_replace('<br>', "\n", $return);
        } else {
            $return = str_replace("\n", '<br>', $return);
        }

        return $return;
    }

}// end class Language